package com.vaadin.thomas.contenttool.ui;

import com.vaadin.event.ShortcutAction.KeyCode;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Button.ClickListener;
import com.vaadin.ui.GridLayout;
import com.vaadin.ui.Label;
import com.vaadin.ui.Window;

public class ConfirmWindow extends Window {

	private static final long serialVersionUID = -9036078228007401774L;

	public interface ConfirmListener {
		public void confirmed();
	}

	private Label question;

	private ConfirmListener listener;

	public ConfirmWindow(ConfirmListener l) {

		listener = l;

		setModal(true);
		setWidth(300, UNITS_PIXELS);
		setIcon(Util.getResource("icons/exclamation"));
		center();

		GridLayout content = new GridLayout(2, 2);
		setContent(content);
		content.setSpacing(true);
		content.setMargin(true);
		content.setWidth(100, UNITS_PERCENTAGE);

		question = new Label();
		question.setWidth(100, UNITS_PERCENTAGE);
		content.addComponent(question, 0, 0, 1, 0);

		Button confirmButton = new Button(AdminMainLayout.ct().get(
				"contenttool.confirm.yes"));
		confirmButton.setIcon(Util.getResource("icons/accept"));
		confirmButton.setWidth(100, UNITS_PERCENTAGE);
		confirmButton.setClickShortcut(KeyCode.ENTER);
		content.addComponent(confirmButton);
		confirmButton.addListener(new ClickListener() {

			private static final long serialVersionUID = -6869413931086226146L;

			@Override
			public void buttonClick(ClickEvent event) {
				listener.confirmed();
				close();
			}
		});

		Button cancelButton = new Button(AdminMainLayout.ct().get(
				"contenttool.confirm.no"));
		cancelButton.setIcon(Util.getResource("icons/cancel"));
		cancelButton.setWidth(100, UNITS_PERCENTAGE);
		cancelButton.addListener(new ClickListener() {

			private static final long serialVersionUID = -7000302260299728744L;

			@Override
			public void buttonClick(ClickEvent event) {
				close();
			}
		});
		content.addComponent(cancelButton);
	}

	public void setQuestion(String s) {
		question.setValue(s);
	}
}
